package divinity.utils.web.loaders
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.ProgressEvent;
	import flash.globalization.DateTimeFormatter;
	import flash.globalization.DateTimeStyle;
	import flash.globalization.LocaleID;
	
	import divinity.utils.log.Log;
	import divinity.utils.web.loaders.DataLoader;
	import divinity.utils.web.types.Types;

	public class Data extends EventDispatcher
	{
		private var _loaders:Array=[];	
		
		private static var _log:Log=new Log("DATA");
		private var _name:String="default";
		private var _loaded:Boolean=true;
		private var _type:String;
		private var _onComplete:Function;		
		/**
		 * <strong>Data</strong> is a loader tool that enables loading of multiple data files with destination parameters.After all files has been dowloaded it calls function referenced by onComplete function and dispatches Event.COMPLETE
		 * 
		 * @see Event.COMPLETE
		 * */
		public function Data(name:String=null)
		{
			super();				
			if(name!=null)
			{
				_name=name;
			}
			addLog("init instance <"+_name+">")
		}
		
		public function addFile(url:String,dest:*,targ:*,type:String=null):void
		{	
			_loaded=false;			
			addLog("Adding file to load 	* "+url+" *")
			var al:DataLoader=new DataLoader();
			al.loadFile(url,dest,targ,type);
			al.addEventListener(Event.COMPLETE,checkLoaded);
			al.addEventListener(ProgressEvent.PROGRESS,checkProgress);
			_loaders.push(al);
		}
		/**
		 * 
		 * Calls function from given parameter after all files has been loaded
		 * @param fnc function that will be called 
		 * */
		public function onComplete(fnc:Function):void
		{
			_onComplete=fnc;
		}
		private function checkProgress(e:ProgressEvent)
		{
			var total:Number=0;
			var loaded:Number=0;
			var pEvent:ProgressEvent;	
			for each(var ldr in _loaders)
			{					
				total+=ldr.fileTotal;
				loaded+=ldr.fileLoaded;
			}
			pEvent=new ProgressEvent(ProgressEvent.PROGRESS,false,false,loaded,total);
			dispatchEvent(pEvent);		
		}
			
		private function checkLoaded(e:Event):void
		{
			var done:Boolean=true
			for each(var loader in _loaders)
			{
				if(!loader.loaded)
				{					
					done=false;
					break;
				}				
			}
			if(done)
			{
				_loaded=true;
				removeListeners();
				addLog("All loaders complete for <"+_name+">");			
				if(_onComplete!=null)
				{
					_onComplete(this)	
				}
				dispatchEvent(new Event(Event.COMPLETE));
			}
		}		
		private function removeListeners():void
		{
			for each(var loader in _loaders)
			{
				loader.removeEventListener(Event.COMPLETE,checkLoaded);
				loader.removeEventListener(ProgressEvent.PROGRESS,checkProgress);			
			}
		}		
		/**
		 * 
		 * 
		 * */
		private function addLog(str:String)
		{
			_log.addLog(this,str);
			
		}
		/*public static function get log():String
		{
			return _log.;
		}*/
		public function get name():String
		{
			return _name;
		}
		public function get loaded()
		{
			return _loaded;
		}
	}
}